package com.lsl.gulimall.product.service.impl;



import com.lsl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lsl.gulimall.product.service.AttrAttrgroupRelationService;
import com.lsl.gulimall.product.service.AttrService;
import com.lsl.gulimall.product.service.CategoryService;
import com.lsl.gulimall.product.vo.request.AttrGroupRelationInVo;
import com.lsl.gulimall.product.vo.request.AttrGroupWithAttrsInVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsl.common.utils.PageUtils;
import com.lsl.common.utils.Query;

import com.lsl.gulimall.product.dao.AttrGroupDao;
import com.lsl.gulimall.product.entity.AttrGroupEntity;
import com.lsl.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrService attrService;
    @Autowired
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if(StringUtils.isNotBlank(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (categoryId==0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);
        }else{
            queryWrapper.eq("catelog_id", categoryId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public AttrGroupEntity getPathById(Long attrGroupId) {
        AttrGroupEntity groupEntity = this.getById(attrGroupId);
        Long catelogId = groupEntity.getCatelogId();

        //通过parentCid获取上级分类
        Long[]Path =categoryService.findCatelogPath(catelogId);
        groupEntity.setCatelogPath(Path);
        return groupEntity;
    }

    @Override
    public void saveAttrGroupRelation(List<AttrGroupRelationInVo> attrGroupRelationInVos) {
        if(attrGroupRelationInVos!=null && attrGroupRelationInVos.size()>0){
            for (AttrGroupRelationInVo attrGroupWithAttrsInVo:attrGroupRelationInVos) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                BeanUtils.copyProperties(attrGroupWithAttrsInVo,attrAttrgroupRelationEntity);
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }
    }

    @Override
    public void deleteRelation(List<AttrGroupRelationInVo> attrGroupRelationInVos) {
        if(attrGroupRelationInVos!=null && attrGroupRelationInVos.size()>0){
            for (AttrGroupRelationInVo attrGroupWithAttrsInVo:attrGroupRelationInVos) {
                QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("attr_id",attrGroupWithAttrsInVo.getAttrId());
                queryWrapper.eq("attr_group_id",attrGroupWithAttrsInVo.getAttrGroupId());
                attrAttrgroupRelationService.remove(queryWrapper);
            }
        }
    }
}